package com.dreamder.service;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.dreamder.model.SonhoBeanTO;
import com.dreamder.util.DreamderConstants;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class SonhoService {

	public String listaSonhosJSON() {
		String retorno = "ERRO";

		try {
			HttpClient client = new DefaultHttpClient();

			HttpGet get = new HttpGet(DreamderConstants.URL_SONHO
					+ "listarSonhos");

			get.setHeader("Content-type", "application/json");

			HttpResponse response = client.execute(get);

			StatusLine status = response.getStatusLine();

			if (status.getStatusCode() == 200) {
				HttpEntity resposta = response.getEntity();
				String respostaEmJSON = EntityUtils.toString(resposta);
				retorno = respostaEmJSON;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retorno;
	}

	public String carregaSonhoJSON(Long id) {
		String retorno = "ERRO";

		try {
			HttpClient client = new DefaultHttpClient();

			HttpPost post = new HttpPost(DreamderConstants.URL_SONHO
					+ "carregaSonho/" + id);

			post.setHeader("Content-type", "application/json");

			HttpResponse response = client.execute(post);

			StatusLine status = response.getStatusLine();

			if (status.getStatusCode() == 200) {
				HttpEntity resposta = response.getEntity();
				String respostaEmJSON = EntityUtils.toString(resposta);
				retorno = respostaEmJSON;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retorno;
	}

	public Boolean salvarSonho(SonhoBeanTO sonho) {

		try {

			HttpClient client = new DefaultHttpClient();

			HttpPost post = new HttpPost(DreamderConstants.URL_SONHO
					+ "cadastrarSonho");

			post.setHeader("Content-type", "application/json");

			/*
			 * JSONStringer json = new JSONStringer(); json.object();
			 * json.key("titulo").value(sonho.getTitulo());
			 * json.key("resumo").value(sonho.getResumo());
			 * json.key("dsDetalhada").value(sonho.getDsDetalhada());
			 * json.key("categoria").value(sonho.getCategoria());
			 * json.key("dono").value(sonho.getDono()); json.endObject();
			 */

			GsonBuilder builder = new GsonBuilder();
			builder.setDateFormat("dd-MM-yyyy");
			Gson gson = builder.create();

			HttpEntity entity = new StringEntity(gson.toJson(sonho));

			post.setEntity(entity);

			HttpResponse response = client.execute(post);

			String resposta = EntityUtils.toString(response.getEntity());

			if (resposta.equals("sonho cadastrado")) {
				return true;
			} else {
				return false;
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}

	}

	public Boolean participarSonho(SonhoBeanTO sonho) {

		try {

			HttpClient client = new DefaultHttpClient();

			HttpPost post = new HttpPost(DreamderConstants.URL_SONHO
					+ "colaborar");

			post.setHeader("Content-type", "application/json");

			GsonBuilder builder = new GsonBuilder();
			builder.setDateFormat("dd-MM-yyyy");
			Gson gson = builder.create();

			HttpEntity entity = new StringEntity(gson.toJson(sonho));

			post.setEntity(entity);

			HttpResponse response = client.execute(post);

			String resposta = EntityUtils.toString(response.getEntity());

			if (resposta.equals("sonho cadastrado")) {
				return true;
			} else {
				return false;
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}

	}
	
	public String pesquisarSonho(String palavra) {
		String retorno = "ERRO";

		try {
			HttpClient client = new DefaultHttpClient();

			HttpGet get = new HttpGet(DreamderConstants.URL_SONHO
					+ "buscarSonho/" + palavra);

			get.setHeader("Content-type", "application/json");

			HttpResponse response = client.execute(get);

			StatusLine status = response.getStatusLine();

			if (status.getStatusCode() == 200) {
				HttpEntity resposta = response.getEntity();
				String respostaEmJSON = EntityUtils.toString(resposta);
				retorno = respostaEmJSON;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retorno;
	}
	
	public String listaSonhosPorUsuario(Long idUsuario) {
		String retorno = "ERRO";

		try {
			HttpClient client = new DefaultHttpClient();

			HttpGet get = new HttpGet(DreamderConstants.URL_SONHO
					+ "buscarSonhoPorUsuario/" + idUsuario);

			get.setHeader("Content-type", "application/json");

			HttpResponse response = client.execute(get);

			StatusLine status = response.getStatusLine();

			if (status.getStatusCode() == 200) {
				HttpEntity resposta = response.getEntity();
				String respostaEmJSON = EntityUtils.toString(resposta);
				retorno = respostaEmJSON;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retorno;
	}

}
